package com.orhon.smartcampus.modules.baseinfo.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.baseinfo.dao.MessageCategoryDao;
import com.orhon.smartcampus.modules.baseinfo.entity.MessageCategory;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消息分类值验证不重复
 */
@Component
public class MessageCategoryValidationFetcher implements DataFetcher {

    @Autowired
    MessageCategoryDao messageCategoryDao;
    @Autowired
    TokenService tokenService;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {

        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);
        String id = env.getArgument("categoryValue").toString();
        String hql = "from MessageCategory as category where category.deletedAt is null and category.categoryValue = "+"'"+id+"'"+" and school_id = "+schoolId;

        MessageCategory messageCategory = messageCategoryDao.findId(hql , null );
        if(messageCategory!=null) {
            return Result.error("该分类已添加，不可重复添加！");
        }
        return Result.ok();
    }

}
